package
{
import com.pblabs.engine.PBE;
import com.pblabs.engine.entity.EntityComponent;

import com.pblabs.engine.entity.IEntity;

import flash.events.KeyboardEvent;
import flash.utils.Dictionary;

public class EntityCreatorComponent extends EntityComponent
      {
            public var entityKeyboardMap:Dictionary = new Dictionary();

            public function EntityCreatorComponent()
            {
            }

            override protected function onAdd():void
            {
                  super.onAdd();
                  PBE.mainStage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
            }

            override protected function onRemove():void
            {
                  super.onRemove();
                  PBE.mainStage.removeEventListener(KeyboardEvent.KEY_UP, onKeyUp);
            }

            protected function removeCurrentEntity():void
            {
                  var existingEntity:IEntity = PBE.nameManager.lookup("DemoEntity");
                  if (existingEntity != null)
                        existingEntity.destroy();
            }

            protected function onKeyUp(event:KeyboardEvent):void
            {
                  var keyCodeString:String = event.keyCode.toString();

                  if (entityKeyboardMap[keyCodeString] != null)
                  {
                        removeCurrentEntity();
                        PBE.templateManager.instantiateEntity(entityKeyboardMap[keyCodeString]);
                  }
            }
      }
}
